dnl #					-*- Sh -*-
dnl # $Id: configure.in,v 1.50 2000-01-27 05:58:49 arensb Exp $
dnl #

dnl # XXX - Test for MAXHOSTNAMELEN, MAXPATHLEN

dnl # XXX - If "tar --version" exits with zero status and contains the
dnl # string "GNU", then it's GNU tar, and we can use
dnl #	GZIP="--best" ${TAR} chozf ${TARBALL} ${DISTDIR}
dnl # Otherwise, fall back on that old stand-by
dnl #	tar cf ${TARBALL} ${DISTDIR}
dnl #	${COMPRESS} ${TARBALL}
dnl # So also check for 'gzip'; if it doesn't exist, fall back on
dnl # 'compress'

AC_INIT(src/coldsync.c)

dnl # Automake does this, but only after all of the variables have
dnl # been defined, which is too late to put the path variables in an
dnl # autogenerated .h file.
test "x$prefix" = xNONE && prefix=${ac_default_prefix}

EXIT_STATUS=0

PACKAGE="coldsync"
VERSION="1.1.2"

dnl # XXX - The three-argument version of AC_DEFINE_UNQUOTED was
dnl # introduced in 2.13, and doesn't work in earlier version (the
dnl # PACKAGE and VERSION macros are defined to be 1 in "config.h").
dnl # Check for this. The check ought to be in m4, so that it'll only
dnl # show up for people who run 'automake', and not people who just
dnl # run the configure file from the distribution.
dnl # The version of automake is in AC_ACVERSION.
AC_DEFINE_UNQUOTED(PACKAGE,["$PACKAGE"],[Name of package])
AC_DEFINE_UNQUOTED(VERSION,["$VERSION"],[Version number of package])
if test x"$EDITION" = x ; then
	EDITION="$VERSION"
fi
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)
AC_SUBST(EDITION)

# This is almost certainly a Good Thing
LDFLAGS="-L${prefix}/lib"
CPPFLAGS="-I${prefix}/include"

## Checks for programs.

AC_PROG_CC
AC_PROG_CXX

# XXX - It ought to be possible to turn off pedantic compilation. But
# this also ought to be cached. How?
#  AC_ARG_ENABLE(pedantic,dnl
#  [  --disable-pedantic      Turn off pedantic compilation])
#  echo "PEDANTIC == [$enable_pedantic]"

# XXX - Some tests (notably AC_C_INLINE) don't work with the anal
# flags turned on. Perhaps this test ought to be moved to the end.

# I know how to be pedantic with gcc. Other compilers, I'm not sure.
if test x"$GCC" = x"yes" ; then
		CFLAGS="$CFLAGS -Wall -ansi -pedantic"
		CXXFLAGS="$CXXFLAGS -Wall -ansi -pedantic"
else
	AC_MSG_RESULT(dnl
[	You don't seem to be using gcc, so various anal retentive
	compiler options won't be turned on. If you know what they
	are for your compiler, you may wish to turn them on, e.g.
		CFLAGS=\"-Wall -ansi -pedantic\" ./configure
		CXXFLAGS=\"-Wall -ansi -pedantic\" ./configure])
fi
AC_SUBST(CFLAGS)

AC_PROG_RANLIB
AC_PROG_INSTALL

dnl # This is basically the same as AC_PROG_LEX, but it uses
dnl # AC_CHECK_PROGS to make sure that there's a working 'lex'.
AC_CHECK_PROGS(LEX, flex lex)
if test -z "$LEX"; then
	AC_MSG_WARN([
	Neither flex nor lex could be found. Please install them, then
	run configure again.])
	LEX=true		# Just in case the bozo tries to build anyway
	EXIT_STATUS=1
fi
if test -z "$LEXLIB"
then
	case "$LEX" in
	    flex*) ac_lib=fl ;;
	    *) ac_lib=l ;;
	esac
	AC_CHECK_LIB($ac_lib, yywrap, LEXLIB="-l$ac_lib")
fi
AC_SUBST(LEXLIB)
AC_SUBST(LEXARGS)		# Not defined by 'configure'. Just a hook.

# I18N (optional, enabled by default)
AC_ARG_WITH(i18n,dnl
[  --without-i18n          Turn off internationalization support.],dnl
:,dnl
with_i18n=yes
)

# The above just checked whether the user asked for --with-i18n, and
# set a default. Now we look for -lintl if necessary
if test x"$with_i18n" != x"no"; then
	AC_CHECK_LIB(intl, gettext)
	if test "$ac_cv_lib_intl_gettext" = yes; then
		# See if the user specified a $LOCALEDIR. If not, use
		# the default.
		if test -z "${LOCALEDIR}"; then
			LOCALEDIR="`eval echo \"${datadir}/locale\"`"
		fi
		AC_DEFINE_UNQUOTED(LOCALEDIR, "${LOCALEDIR}")
	fi
fi

# I'm not using AC_PROG_YACC here because that doesn't actually make
# sure that there is a working yacc.
AC_CHECK_PROGS(YACC, 'bison -y' byacc yacc)
AC_SUBST(YACCARGS)		# Not defined by 'configure'. Just a hook.
if test -z "$YACC"; then
	AC_MSG_WARN([
	Neither yacc nor bison could be found. Please install them, then
	run configure again.])
	YACC=true		# Just in case the bozo tries to build anyway
	EXIT_STATUS=1
fi

# AIX sucks so bad, it requires its own special check.
AC_AIX

# XXX - Fails due to -Wall
dnl # AC_C_INLINE

AC_CHECK_PROGS(DATE, date)
AC_CHECK_PROGS(MKDEP,[mkdep makedepend], true)
AC_CHECK_PROGS(MKID, mkid, true)
AC_CHECK_PROGS(TAR,[gtar tar])
AC_CHECK_PROGS(GZIP,[gzip compress])
AC_CHECK_PROGS(LINT,lint)

if test -z "$LINTARGS"; then
	LINTARGS="-hxFH"
fi
AC_SUBST(LINTARGS)

# XXX - I think there are other programs called 'makeinfo'. It would
# be nice to make sure that this is the right one. It's probably
# enough to run '$MAKEINFO --version' and grep for "GNU" in the
# output.
AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo, true)
if test -z "$ac_cv_prog_MAKEINFO" -o x"$ac_cv_prog_MAKEINFO" = x"true"; then
	AC_MSG_WARN([
	The makeinfo program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROGS(TEXI2DVI, [texi2dvi tex], true)
if test -z "$ac_cv_prog_TEXI2DVI" -o x"$ac_cv_prog_TEXI2DVI" = x"true"; then
	AC_MSG_WARN([
	The texi2dvi program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROG(DVIPS, dvips, dvips, true)
if test -z "$ac_cv_prog_DVIPS" -o x"$ac_cv_prog_DVIPS" = x"true"; then
	AC_MSG_WARN([
	The dvips program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROGS(TEXI2HTML, texi2html, true)
if test -z "$ac_cv_prog_TEXI2HTML" -o x"$ac_cv_prog_TEXI2HTML" = x"true"; then
	AC_MSG_WARN([
	The texi2html program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

## Checks for libraries.

# XXX - RedHat 6.0 has gethostbyname() in libnsl, but also has it in
# libc. This test adds "-lnsl" to $LIBS, even though it isn't
# required.
AC_CHECK_LIB(nsl,gethostbyname,
	LIBS="$LIBS -lnsl",
	# Under UnixWare 7.1.0, gethostbyname() is in libsocket.
	AC_CHECK_LIB(socket,gethostbyname))
AC_CHECK_LIB(y, yyerror, LIBYACC="-ly")
AC_SUBST(LIBYACC)

# ElectricFence (optional)
AC_ARG_WITH(efence,dnl
[  --with-efence           Use Electric Fence memory checking library
                            (only desirable for developers).],dnl
[AC_CHECK_LIB(efence,malloc)
if test "$ac_cv_lib_efence_malloc" = yes; then
	AC_DEFINE_UNQUOTED(WITH_EFENCE)
else
	AC_MSG_WARN([Can't find ElectricFence library!])
	#' # For Emacs
fi])

## Checks for header files.

AC_HEADER_STDC
AC_HEADER_DIRENT
CS_DIRENT_TYPE
AC_CHECK_HEADERS([\
	fcntl.h \
	locale.h \
	strings.h \
	termios.h \
	unistd.h \
	sys/select.h \
	sys/time.h \
	sys/types.h
])

#echo "ac_cv_header_blah_h == <$ac_cv_header_blah_h>"	# <>
#echo "ac_cv_header_fcntl_h == <$ac_cv_header_fcntl_h>"	# <yes>

## Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIGNAL
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_HEADER_TIME
dnl AC_STRUCT_TM
AC_STRUCT_TIMEZONE

## Checks for library functions.
AC_CHECK_FUNCS(getopt \
	memcpy \
	select \
	rename \
	strchr \
	strncpy \
	cfmakeraw \
	strcasecmp \
	strncasecmp \
	sysconf
)

# Optional USB support
# (Only works under FreeBSD 4.x, for now)
AC_ARG_WITH(usb,dnl
[  --with-usb              Enable USB support for Visor (experimental,
                             FreeBSD 4.x only).],
	# XXX - Should check to see whether /sys/dev/usb/usb.h exists,
	# whether it contains 'struct usb_device_info', whether that
	# struct contains a "release" field, and whether the cpp
	# symbol UT_READ_VECTOR_ENDPOINT is defined.
	WITH_USB="$withval"
	if test x"$WITH_USB" != xno; then
		AC_DEFINE_UNQUOTED(WITH_USB)
	fi
)

# Print error messages for various required bits that couldn't be found.
if test -z "$ac_cv_func_getopt" -o x"$ac_cv_func_getopt" = x"no" ; then
	AC_MSG_WARN([
	The getopt() function not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

if test -z "$ac_cv_func_select" -o x"$ac_cv_func_select" = x"no" ; then
	AC_MSG_WARN([
	The select() function not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

AC_DEFINE_UNQUOTED(SYSCONFDIR,"`eval echo \"${sysconfdir}\"`")

AC_CONFIG_HEADER(config.h)
AC_OUTPUT([Make.rules
	doc/version.texi])
# XXX - I'm not sure this is working quite right everywhere just yet
#  AC_MSG_RESULT(dnl
#  [
#  	Don't forget to run \"make depend\"
#  ])
dnl #'	# For Emacs
exit "$EXIT_STATUS"
